<template>
  <div>
    <div class="articleList">
      <div v-for="item in this.article" :key="item.id">
        <h2 class="title">{{ item.title }}</h2>
        <div class="articleType">
          <i class="el-icon-date"></i>
          {{ item.addTime }}
          <i class="el-icon-view"></i> {{ item.view_count }}人
          <i class="el-icon-folder"></i> {{ item.typeName }}
        </div>
        <el-divider></el-divider>
        <div class="articleInfo">
          <div v-html="item.content"></div>
        </div>
      </div>
    </div>
    <div class="lyb">
      <h2>留言板</h2>
      <el-form ref="form" :model="form" label-width="45px">
        <el-form-item label="昵称" class="formWidth">
          <el-input placeholder="请输入您的昵称" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" class="formWidth" label-width="45px">
          <el-input
            placeholder="请输入您的邮箱"
            v-model="form.email"
          ></el-input>
        </el-form-item>
        <el-input
          type="textarea"
          placeholder="请输入您想说的话"
          v-model="form.content"
        ></el-input>
        <el-button class="sublime" type="primary" @click="onSubmit"
          >留言</el-button
        >
      </el-form>
    </div>
    <div class="messageList" v-if="message.length > 0">
      <div class="title">最新留言</div>
      <div v-for="item in message" :key="item.msg_id">
        <div class="content">{{ item.content }}</div>
        <div class="nitime">
          <div>昵称：{{ item.name }}</div>
          <div>时间：{{ timestampToTime(item.lytime) }}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getArticleById, getMessage, insterMess } from "@/api/article";
export default {
    async mounted() {
        this.article = await getArticleById(this.$route.query.id);
        this.message = await getMessage(this.$route.query.id);
    },
    watch: {
        async $route(to, from) {
            this.article = await getArticleById(to.query.id);
			this.message = await getMessage(this.$route.query.id);
        },
    },
    data() {
        return {
            article: [],
            form: {
                name: "",
                email: "",
                content: "",
            },
            message: [],
        };
    },
    methods: {
        async onSubmit() {
            //console.log(this.form);
			let lytime =  Date.parse( new Date() ).toString().substr(0,10);
			let data = {
				...this.form,
				lytime:lytime,
				article_id: this.$route.query.id
			}
			await insterMess(data).then(
               this.$message({
                    message: '留言成功！',
                    type: 'success'
                })
            ),
			this.message = await getMessage(this.$route.query.id);
			this.form = {
				name: "",
                email: "",
                content: ""
			}
        },
        timestampToTime(timestamp) {
            var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
            var Y = date.getFullYear() + "-";
            var M =
                (date.getMonth() + 1 < 10
                    ? "0" + (date.getMonth() + 1)
                    : date.getMonth() + 1) + "-";
            var D =
                (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) +
                " ";
            var h =
                (date.getHours() < 10
                    ? "0" + date.getHours()
                    : date.getHours()) + ":";
            var m =
                (date.getMinutes() < 10
                    ? "0" + date.getMinutes()
                    : date.getMinutes()) + ":";
            var s =
                date.getSeconds() < 10
                    ? "0" + date.getSeconds()
                    : date.getSeconds();
            return Y + M + D + h + m + s;
        },
    },
};
</script>
<style lang="less" scoped>
.articleList {
  padding: 10px;
  border-bottom: 2px solid #ccc;
  margin: 12px 0;
  & .title {
    padding-bottom: 20px;
    text-align: center;
    color: rgb(24, 144, 252);
  }
  .articleType {
    text-align: center;
  }
  & .articleType i:not(:nth-child(1)) {
    margin-left: 15px;
  }
  & .articleInfo {
    padding-bottom: 10px;
    color: rgb(0, 05, 05);
  }
}
.lyb {
  background: rgb(243, 243, 243);
  padding: 15px;
  border-radius: 10px;
  & h2 {
    text-align: center;
    height: 32px;
  }
  & .formWidth {
    width: 50%;
    display: inline-block;
  }
  & .sublime {
    width: 100%;
    margin-top: 10px;
  }
}
.messageList {
  margin: 20px;

  & .title {
    font-size: 24px;
    padding: 15px;
    border-bottom: 2px solid #ccc;
  }
  & .content {
    font-size: 18px;
    line-height: 30px;
    padding: 5px;
  }
  & .nitime {
    display: flex;
    padding: 5px;
    border-bottom: 1px solid #ccc;
    & div {
      width: 50%;
    }
  }
}
</style>
